//
//  RecommendCustomViewController.m
//  NBTripe
//
//  Created by Lee on 16/1/12.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "RecommendCustomViewController.h"
#import "CustomDetailTextModel.h"
#import "CustomDetailTextTableViewCell.h"
#import "PhotoAlbumViewController.h"
#import "MapViewController.h"
#import "CustomMapModel.h"

@interface RecommendCustomViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableview;
//days大数组
@property (nonatomic, strong) NSMutableArray *detailTextArr;
//重装大数组
@property (nonatomic, strong) NSMutableArray *mainArr;


@property (nonatomic,strong) CustomMapModel *customMapModel;

@property (nonatomic,strong) UIImageView *mapImageView;

@property (nonatomic, retain) UIImageView *userImage;

@property (nonatomic,strong) UILabel *userName;

@property (nonatomic,strong) UILabel *titleLabel;

@property (nonatomic,strong) UILabel *dateLabel;

@property (nonatomic,strong) UILabel *dayLabel;

@property (nonatomic,strong) UILabel *howLong;

@property (nonatomic,strong) UILabel *numberOfLike;





@end

@implementation RecommendCustomViewController

- (void)viewDidLoad {
    self.navigationController.navigationBar.translucent = NO;
    
    self.detailTextArr = [NSMutableArray array];
   
    self.mainArr = [NSMutableArray array];
    [super viewDidLoad];
    
    [self createTableView];
    [self getData];
    
    // Do any additional setup after loading the view.
}

/**
 * 获取cell数据
 */
- (void)getData {

    [XYNetWorkTool getWithUrl:kRecommendCustomDetailURL(_joinedId) parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = result;
        self.customMapModel = [[CustomMapModel alloc] initWithDataSource:dic];
        NSArray *array = [dic objectForKey:@"days"];
        for (NSDictionary *dic in array) {
            CustomDetailTextModel *model = [[CustomDetailTextModel alloc] initWithDataSource:dic];
            //外部数组
            [self.detailTextArr addObject:model];
            for (NSDictionary *secondic in [dic objectForKey:@"waypoints"]) {
                CustomDaysWayPointsModel *model = [[CustomDaysWayPointsModel alloc] initWithDataSource:secondic];
                //内部数组
                [self.mainArr addObject:model];
               // NSLog(@"%@", self.mainArr);
            }
      
        }
        [self.mapImageView sd_setImageWithURL:[NSURL URLWithString:self.customMapModel.trackpoints_thumbnail_image]];
        [self.userImage sd_setImageWithURL:[NSURL URLWithString:self.customMapModel.userModel.avatar_l]];
        self.userName.text = [NSString stringWithFormat:@"by %@",self.customMapModel.userModel.name];
        self.titleLabel.text = self.customMapModel.name;
        self.dateLabel.text = self.customMapModel.first_day;
        self.dayLabel.text = [NSString stringWithFormat:@"%ld天",self.customMapModel.days.count];
        self.howLong.text = [NSString stringWithFormat:@"%dkm",self.customMapModel.mileage.intValue];
        self.numberOfLike.text = [NSString stringWithFormat:@"%d", self.customMapModel.recommendations.intValue];
        [self.tableview reloadData];
        }
     fail:nil];
}

/**
 *  创建tableview
 */
- (void)createTableView {
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT) style:UITableViewStylePlain];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [self.tableview registerClass:[CustomDetailTextTableViewCell class] forCellReuseIdentifier:@"CustomDetailTextTableViewCell"];
    self.tableview.separatorStyle =  UITableViewCellSeparatorStyleNone;
    [self createHeaderView];
    [self.view addSubview:self.tableview];
}

-(void)createHeaderView {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 350)];
    headerView.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.87 alpha:1];
    self.mapImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 200)];
    [self.mapImageView setImage:[UIImage imageNamed:@"160.jpg"]];
    self.mapImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didPressView)];
    [self.mapImageView addGestureRecognizer:tap];
    [headerView addSubview:self.mapImageView];
    
    self.userImage = [[UIImageView alloc]initWithFrame:CGRectMake( 100, 40, 50, 50)];
    self.userImage.centerX = self.view.centerX;
    self.userImage.centerY = self.mapImageView.bottom;
    [self.userImage setImage:[UIImage imageNamed:@"160.jpg"]];
    _userImage.layer.borderWidth = 4;
    _userImage.layer.borderColor = [UIColor whiteColor].CGColor;
    _userImage.layer.masksToBounds = YES;
    _userImage.layer.cornerRadius = 25;
    [headerView addSubview:self.userImage];
    
    self.userName = [[UILabel alloc]initWithFrame:CGRectMake(0, self.userImage.bottom , self.view.width, 20)];
    self.userName.centerX = self.view.centerX;
    _userName.textAlignment = NSTextAlignmentCenter;
    _userName.textColor = [UIColor grayColor];
    _userName.font = [UIFont systemFontOfSize:10];
    [headerView addSubview:self.userName];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.userName.bottom , self.view.width, 30)];
    self.titleLabel.centerX = self.view.centerX;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.font = [UIFont systemFontOfSize:17];
    self.titleLabel.shadowColor = [UIColor grayColor];
    [headerView addSubview:self.titleLabel];
    
    UILabel *xian = [[UILabel alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom , self.view.width, 20)];
    xian.centerX = self.view.centerX;
    xian.textAlignment = NSTextAlignmentCenter;
    xian.textColor = [UIColor grayColor];
    xian.text = @"———————————————————";
    [headerView addSubview:xian];
    
    UILabel *liCheng = [[UILabel alloc]initWithFrame:CGRectMake(0, xian.bottom ,80, 20)];
    liCheng.centerX = self.view.centerX;
    liCheng.textAlignment = NSTextAlignmentCenter;
    liCheng.text = @"里程";
    liCheng.textColor = [UIColor blackColor];
    liCheng.font = [UIFont systemFontOfSize:10];
    [headerView addSubview:liCheng];
    
    UIImageView *geduan1 = [[UIImageView alloc] initWithFrame:CGRectMake(liCheng.left - 10, liCheng.top ,10, 50)];
    geduan1.image = [UIImage imageNamed:@"dianxian"];
    [headerView addSubview:geduan1];
    
    UIImageView *geduan2 = [[UIImageView alloc] initWithFrame:CGRectMake(liCheng.right , liCheng.top ,10, 50)];
    geduan2.image = [UIImage imageNamed:@"dianxian"];
    [headerView addSubview:geduan2];
    
    UILabel *like = [[UILabel alloc]initWithFrame:CGRectMake(geduan2.right , xian.bottom ,80, 20)];
    like.textAlignment = NSTextAlignmentCenter;
    like.text = @"喜欢";
    like.textColor = [UIColor blackColor];
    like.font = [UIFont systemFontOfSize:10];
    [headerView addSubview:like];
    
    
    self.dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(geduan1.left - 100 , xian.bottom ,80, 20)];
    self.dateLabel.textAlignment = NSTextAlignmentCenter;
    self.dateLabel.textColor = [UIColor blackColor];
    self.dateLabel.font = [UIFont systemFontOfSize:10];
    [headerView addSubview:self.dateLabel];
    
    self.dayLabel = [[UILabel alloc]initWithFrame:CGRectMake(geduan1.left - 100 , self.dateLabel.bottom + 10 ,80, 20)];
    self.dayLabel.textAlignment = NSTextAlignmentCenter;
    self.dayLabel.textColor = [UIColor blackColor];
    self.dayLabel.font = [UIFont systemFontOfSize:10];
    [headerView addSubview:self.dayLabel];
    
    self.howLong = [[UILabel alloc]initWithFrame:CGRectMake(0 , self.dateLabel.bottom + 10 ,80, 20)];
    self.howLong.centerX = self.view.centerX;
    self.howLong.textAlignment = NSTextAlignmentCenter;
    self.howLong.textColor = [UIColor blackColor];
    self.howLong.font = [UIFont systemFontOfSize:10];
    [headerView addSubview:self.howLong];
    
    self.numberOfLike = [[UILabel alloc]initWithFrame:CGRectMake(geduan2.right , self.dateLabel.bottom + 10 ,80, 20)];
    self.numberOfLike.textAlignment = NSTextAlignmentCenter;
    self.numberOfLike.textColor = [UIColor blackColor];
    self.numberOfLike.font = [UIFont systemFontOfSize:10];
    [headerView addSubview:self.numberOfLike];


  
     self.tableview.tableHeaderView = headerView;

}
/**
 *  点击头视图
 */
- (void)didPressView {
    MapViewController *mapviewVC = [[MapViewController alloc] init];
    mapviewVC.join_id = self.joinedId;
    [self.navigationController pushViewController:mapviewVC animated:YES];
}


/**
 *  tableview协议方法
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return _mainArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自适应高度
    NSString *context = [_mainArr[indexPath.row] text];
    CGRect rect = [context boundingRectWithSize:CGSizeMake(WIDTH-20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    return 300*XHIGHT+rect.size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomDetailTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomDetailTextTableViewCell"];
    //获取数组的第几个元素
    cell.row = indexPath.row;
    //最里层model
    cell.waypointmodel = self.mainArr[indexPath.row];
  
        NSInteger day = [self.mainArr[indexPath.row] day] ;
    NSString *date = [self.mainArr[indexPath.row] local_time];
    cell.daylabel.text = [NSString stringWithFormat:@"第%ld天 %@", day, date];
    /**
     *  block传值给相册
     */
      void (^Block)(NSInteger) = ^(NSInteger row){
        PhotoAlbumViewController *photoVC= [[PhotoAlbumViewController alloc] init];
        photoVC.informationArray = self.mainArr;
        photoVC.row = row;
        [self presentViewController:photoVC animated:YES completion:^{
            
        }];
    };
    cell.block = Block;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
